package org.example;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;

public class ArticleContentScraper {

    private static final Logger logger = LoggerFactory.getLogger(ArticleContentScraper.class);
    private static final int MAX_RETRIES = 3; // 最大重试次数

    // 使用 Selenium 爬取文章内容
    public String fetchArticleContent(String url) {
        logger.info("正在爬取文章内容：{}", url);

        // 配置 WebDriver（使用 Chrome 浏览器）
        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless", "--disable-gpu", "--no-sandbox",
                "start-maximized", "disable-infobars", "--disable-extensions",
                "--disable-dev-shm-usage", "--blink-settings=imagesEnabled=false"); // 禁止图片加载

        int attempt = 0; // 当前尝试次数
        String finalContent = "";

        while (attempt < MAX_RETRIES) {
            WebDriver driver = new ChromeDriver(options);
            try {
                logger.info("尝试第 {} 次爬取", attempt + 1);

                // 打开目标页面
                driver.get(url);

                // 显式等待，确保文章内容加载完成
                WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
                WebElement articleContent = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.index_cententWrap__Jv8jK")));

                if (articleContent != null) {
                    StringBuilder content = new StringBuilder();
                    for (WebElement p : articleContent.findElements(By.tagName("p"))) {
                        content.append(p.getText()).append("\n");
                    }
                    finalContent = content.toString().trim(); // 去除多余换行
                }

                if (!finalContent.isEmpty()) {
                    logger.info("文章内容获取成功");
                    return finalContent;
                } else {
                    logger.warn("文章内容为空，准备重试...");
                }
            } catch (Exception e) {
                logger.error("发生异常: {}", e.getMessage(), e);
            } finally {
                driver.quit(); // 确保浏览器关闭
            }
            attempt++;
        }

        return "无法获取内容（已重试 " + MAX_RETRIES + " 次）";
    }
}
